// NO (INTER-)DEPENDENCIES!

/* List of all the "API" functions:
 * note_progress(): shows progress
 * and needs no initialization; it
 * cannot be reinitialized yet...
*/

/* ------------------------
| Compilation Necessities |
-------------------------*/

#include <iostream>
#include <cmath>

/* -----------------
| Code starts now! |
----------------- */

// Code to mark progress achieved.
// CODED IN HASTE AS NON-CRITICAL.
// STICK TO TESTED VALUES* ONLY.
unsigned note_progress( const unsigned& len,
		const bool& reset = false, const float& knum_init = 0 ){

	// indicate = true => show progress (via stderr,
	// intended for use when piped with wc, or
	// stdout is redirected to a file, etc.).
	// find = true => show progress (in stdout, to
	// see where - in the log - the code gets stuck).
	static const bool indicate = true, find = false;

	// Show fine-grained progress (in %).
	// *Note: -1, 0, 1 are usable values.
	static const int decimals = 0;

	static const double nume = pow( 10, decimals );
	static const double deno = nume * 10;

	// Code to remember the past progress.
	static unsigned progress, rounded, reached;
	if( reset ){ progress = 0; rounded = 0; reached = 0; }

	// Total length of the log
	// (in thousands of lines).
	static float knum;
	if( reset ){ knum = knum_init; }

	// Code to evaluate current progress
	// and indicate it if "significant".
	unsigned check = progress*nume/knum;
	if( check%10 == 0 )
		if( check > rounded ){

			rounded = check;
			double percent = rounded/deno;

			// Print a zero if only single
			// digit progress achieved yet.
			if( percent < 10 ){
				if( indicate ) std::cout << "0";
				if( find ) std::cerr << "0";
			}

			if( indicate ) std::cout << percent << "%";
			if( find ) std::cerr << percent << "%";

			// Print an endline character after
			// every 10 progress indications...
			if( reached == 9 ){
				if( indicate ) std::cout << "\n";
				if( find ) std::cerr << "\n";
				reached = 0;
			}
			else{
				if( indicate ) std::cout << " ";
				if( find ) std::cerr << " ";
				reached++;
			}

			cout.flush(); cerr.flush();

		}

	progress += len;

	return reached;

}
